package com.jz.community.moduleshopping.address.task;

import android.app.Activity;

import com.jz.community.basecomm.constant.AppConstants;
import com.jz.community.basecomm.constant.Constant;
import com.jz.community.basecomm.net.OkHttpUtil;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.task.RxTask;
import com.jz.community.basecomm.utils.BaseSpUtils;
import com.jz.community.basecomm.utils.JsonUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.commview.view.widget.ProgressDialogManager;
import com.jz.community.moduleshopping.address.bean.AdCodeInfo;
import com.jz.community.moduleshopping.address.bean.UserAddressInfo;

import java.util.HashMap;

/**
 * Created by Target on 2018/5/9.
 */
public class GetAdCodeTask extends RxTask<String, Integer, AdCodeInfo> {

    private Activity activity;
    private ITaskCallbackListener taskListener;

    public GetAdCodeTask(Activity activity,
                         ITaskCallbackListener taskListener) {
        super(activity);
        this.activity = activity;
        this.taskListener = taskListener;
    }

    @Override
    protected AdCodeInfo doInBackground(String... params) {
        // 执行请求
        String result = OkHttpUtil.get(Constant.HTTP_ADCODE_URL_BASE_HEAD, getParam(params[0], params[1]));
        AdCodeInfo info = null;
        if (!Preconditions.isNullOrEmpty(result)) {
            info = JsonUtils.parseObject(result, AdCodeInfo.class);
        }
        return info;
    }

    @Override
    protected void onPostExecute(AdCodeInfo result) {
        // 执行结果回调函数
        taskListener.doTaskComplete(result);
        ProgressDialogManager.dismissProgressDialog();
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        ProgressDialogManager.showDialog(activity);
        super.onPreExecute();
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    /**
     * 获取请求所需的参数
     *
     * @param
     * @return
     */
    private HashMap<String, Object> getParam(String address, String city) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("address", address);
        param.put("key", AppConstants.CITY_CODE_ID);
        param.put("city", city);
        return param;
    }
}
